package com.maybach.web.framework.util;

import com.maybach.web.framework.SerializableBindingResult;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import org.springframework.web.util.UriComponentsBuilder;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by houenxun on 15/12/9.
 */
public class WebUtil {
    /**
     * 是否是ajax请求
     *
     * @param req
     * @return
     */
    public static boolean isJson(HttpServletRequest req) {
        return req.getRequestURI().endsWith(".json");
    }

    /**
     * 获取 redirectURL
     *
     * @param httpRequest
     * @param path
     * @return
     *//*
    public static String getRedirectUrl(HttpServletRequest httpRequest, String path) {
        UriComponentsBuilder builder = ServletUriComponentsBuilder.fromContextPath(httpRequest).path(path).queryParam(FerrariConstant.REDIRECT_URL, ServletUriComponentsBuilder.fromRequest(httpRequest).build().encode().toUriString());
        return builder.build().encode().toUriString();
    }
*/
    /**
     * 获取 redirectURL，跳转URL为 referer
     *
     * @param httpRequest
     * @param path
     * @return
     */
    public static String getRedirectUrlFromReferer(HttpServletRequest httpRequest, String path) {
        UriComponentsBuilder builder = ServletUriComponentsBuilder.fromContextPath(httpRequest).path(path).queryParam("redirectURL", httpRequest.getHeader("referer"));
        return builder.build().encode().toUriString();
    }

    /**
     * 返回上一次请求的页面
     * @param httpRequest
     */
    public static String getLastUrlFromReferer(HttpServletRequest httpRequest){
        return httpRequest.getHeader("referer");
    }

    /**
     * 临时存储flash属性
     * @param redirectAttributes
     * @param model
     * @return
     */
    public static RedirectAttributes storeFlashAttribute(RedirectAttributes redirectAttributes ,ModelMap model){
        for(String key: model.keySet()){
            Object object = model.get(key);
            if(object instanceof BindingResult){
                redirectAttributes.addFlashAttribute(key, new SerializableBindingResult((BindingResult)object));
            }else{
                redirectAttributes.addFlashAttribute(key, object);
            }
        }
        return redirectAttributes;
    }

    /**
     * 临时存储flash属性
     * @param redirectAttributes
     * @param model
     * @return
     */
    public static RedirectAttributes storeFlashAttribute(RedirectAttributes redirectAttributes ,Model model){
        for(Map.Entry<String, Object> entry: model.asMap().entrySet()){
            Object object = entry.getValue();
            if(object instanceof BindingResult){
                redirectAttributes.addFlashAttribute(entry.getKey(), new SerializableBindingResult((BindingResult)object));
            }else{
                redirectAttributes.addFlashAttribute(entry.getKey(), object);
            }
        }
        return redirectAttributes;
    }
}
